//
//  CustomerSerController.m
//  LiquorChain
//
//  Created by apple on 2019/1/4.
//  Copyright © 2019年 王冬. All rights reserved.
//

#import "CustomerSerController.h"
@interface CustomerSerController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UITextView *textView;
@property (nonatomic,strong)UILabel *numberLabel;
@property (nonatomic,strong) UIButton *commitBtn;
@end

@implementation CustomerSerController
static NSString *CellIdentifier = @"customerCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"客服";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, kNavBarHeight +15, SCREEN_WIDTH, SCREEN_HEIGHT - kNavBarHeight - 15- kIPhoneXBottomHeight ) style: UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = BACKGROUNDCOLOR;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:self.tableView];
    [self creatHeadView];
    // Do any additional setup after loading the view.
}
-(void)creatHeadView{
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 284)];
    headView.backgroundColor = BACKGROUNDCOLOR;
    UIView *bodyView =[[UIView alloc]init];
    [headView addSubview:bodyView];
    bodyView.backgroundColor = [UIColor whiteColor];
    bodyView.layer.cornerRadius = 5;
    [bodyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(17);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(180);
    }];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:nil];
    self.textView =[[UITextView alloc]init];
    self.textView.font =[UIFont systemFontOfSize:15];
    self.textView.delegate = self;
    self.textView.placeholder = @"请输入您的问题,我们将尽快给您回复";
    [bodyView addSubview:self.textView];

    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0); 
        make.height.mas_equalTo(150);
    }];
    UILabel *numberLabel1 =[[UILabel alloc]init];
    numberLabel1.text = @"/200";
   // numberLabel.backgroundColor =[UIColor redColor];
    numberLabel1.font =[UIFont systemFontOfSize:14];
    numberLabel1.textColor = [UIColor grayColor];
    [bodyView addSubview:numberLabel1];
    [numberLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(160);
        make.height.mas_equalTo(14);
        //make.width.mas_equalTo(30);
        
    }];
    UILabel *numberLabel =[[UILabel alloc]init];
    self.numberLabel = numberLabel;
    numberLabel.textColor = [UIColor grayColor];
    numberLabel.text = @"0";
    numberLabel.font =[UIFont systemFontOfSize:14];
    [bodyView addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(160);
        make.right.mas_equalTo(numberLabel1.mas_left).with.offset(-1);
        make.height.mas_equalTo(14);

    }];
    
    self.commitBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [headView  addSubview:self.commitBtn];
    [self.commitBtn setTitle:@"确认" forState:UIControlStateNormal];
    [self.commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.commitBtn.backgroundColor = CUSTOMCOLOR;
    self.commitBtn.layer.cornerRadius = 5;
    [self.commitBtn addTarget:self action:@selector(commitClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(bodyView.mas_bottom).with.offset(15);
        make.height.mas_equalTo(50);
    }];
    
    self.tableView.tableHeaderView = headView;
}
-(void)commitClick:(UIButton *)sender{
    NSLog(@"提交客服信息");
}
/// 控制字数限制
- (void)textViewEditChanged:(NSNotification*)obj {
    UITextView *textView = self.textView;
    NSString *textStr = textView.text;
    NSInteger fontNum = 200 - textStr.length;
    fontNum = fontNum < 0 ? 0 : fontNum;
    self.numberLabel.text = [NSString stringWithFormat:@"%@",@(fontNum)];
    if (textStr.length > 200) {
        textView.text = [textStr substringToIndex:200];
    }
}
#pragma mark - UITextViewDelegate
//- (void)textViewDidEndEditing:(UITextView *)textView
//{
//    if(textView.text.length < 1){
//        textView.text = @"请输入您的问题,我们会尽快给您回复";
//        textView.textColor = [UIColor groupTableViewBackgroundColor];
//    }
//}
//- (void)textViewDidBeginEditing:(UITextView *)textView
//{
//    if([textView.text isEqualToString:@"请输入您的问题,我们会尽快给您回复"]){
//        textView.text=@"";
//        textView.textColor=[UIColor blackColor];
//    }
//}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    
    UIImageView *titleImageView =[[UIImageView alloc]init];
    [headView addSubview:titleImageView];
    //titleImageView.backgroundColor =[UIColor greenColor];
    titleImageView.image =[UIImage imageNamed:@"wen"];
    [titleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.height.width.mas_equalTo(24);
        make.left.mas_equalTo(15);
    }];
    UILabel *titleLabel =[[UILabel alloc]init];
    [headView addSubview:titleLabel];
    titleLabel.text = @"新手操作注意事项有哪些?";
    titleLabel.font =[UIFont systemFontOfSize:15];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(titleImageView.mas_right).with.offset(10);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(50);
        
    }];
    return headView;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = @"新手操作注意事项有哪些新手操作注意事项有哪些新手操作注意事项有哪些新手操作注意事项有哪些新手操作注意事项有哪些新手操作注意事项有哪些新手操作注意事项有哪些";
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font =[UIFont systemFontOfSize:14];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

@end
